﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyBlog.Common;
using MyBlog.Model;
using Webdiyer.WebControls.Mvc;

namespace MyBlog.Web.Controllers
{
    public class WordsController : BaseController
    {
        [TrackRequest]
        public ActionResult Index()
        {
            var all = XiaoBoKe.WordsService.GetAll();
            ViewBag.TotalPage = all.Count() % 15 == 0 ? (all.Count / 15) : (all.Count / 15 + 1);
            ViewBag.total = all.Count;
            return View();
        }

        public PartialViewResult _WordAjaxList(int page = 1)
        {
            var wordlist = XiaoBoKe.WordsService.GetAll().OrderByDescending(x => x.CreateTime).ToPagedList(page, 15);
            return PartialView(wordlist);
        }

        [HttpPost]
        [Authenticate(false)]
        public JsonResult Release(WordsModel model)
        {
            model.UserID = UserSession.AccountId;
            if (model.ID == 0)
            {
                XiaoBoKe.WordsService.Add(model);
            }
            else
            {
                XiaoBoKe.WordsService.Update(model);
            }
            return Json(new JsonData { State = AjaxState.Success });
        }

        [Authenticate(false)]
        [HttpPost]
        public JsonResult Agree(int id)
        {
            var model = XiaoBoKe.WordsService.Get(id);
            if (string.IsNullOrWhiteSpace(model.AgreeUsers) || (!string.IsNullOrWhiteSpace(model.AgreeUsers) && !model.AgreeUsers.Contains(UserSession.AccountId)))
            {
                XiaoBoKe.WordsService.AddAgree(id, UserSession.AccountId);
                return Json(new JsonData { State = AjaxState.Success });
            }
            else
            {
                return Json(new JsonData { State = AjaxState.Fail, Message = "你已经点过赞啦！" });
            }
        }

        [Authenticate(false)]
        [HttpPost]
        public JsonResult AgreeReply(int id)
        {
            var model = XiaoBoKe.WordCommentService.Get(id);
            if (string.IsNullOrWhiteSpace(model.AgreeUsers) || (!string.IsNullOrWhiteSpace(model.AgreeUsers) && !model.AgreeUsers.Contains(UserSession.AccountId)))
            {
                XiaoBoKe.WordCommentService.AddAgree(id, UserSession.AccountId);
                return Json(new JsonData { State = AjaxState.Success });
            }
            else
            {
                return Json(new JsonData { State = AjaxState.Fail, Message = "你已经点过赞啦！" });
            }
        }

        [HttpPost]
        [Authenticate(false)]
        public JsonResult AddComment(int id, string content)
        {
            var comment = new WordCommentModel { WID = id, Content = content, CommentUID = UserSession.AccountId };
            XiaoBoKe.WordCommentService.Add(comment);
            return Json(new JsonData { State = AjaxState.Success });
        }

        [HttpPost]
        [Authenticate(false)]
        public JsonResult Deleteword(int id)
        {
            var word = XiaoBoKe.WordsService.Get(id);
            if (word.WordComments.Count > 0)
            {
                foreach (var item in word.WordComments)
                {
                    XiaoBoKe.WordCommentService.Delete(item.ID);
                }
            }
            XiaoBoKe.WordsService.Delete(id);
            return Json(new JsonData { State = AjaxState.Success });
        }

        [HttpPost]
        [Authenticate(false)]
        public JsonResult DelComment(int id)
        {
            XiaoBoKe.WordCommentService.Delete(id);
            return Json(new JsonData { State = AjaxState.Success });
        }

        public PartialViewResult _WordEdit(int id)
        {
            var model = XiaoBoKe.WordsService.Get(id);
            return PartialView(model);
        }
    }
}
